package demo.rest.portal;

import demo.common.RestResponseEnum;
import demo.entity.Register;
import demo.response.CompetitionProblemVO;
import demo.response.RestResponseVO;
import demo.service.CompetitionProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/competitionProblem")
@Controller
public class CompetitionProblemController {

    @Autowired
    private CompetitionProblemService competitionProblemService;

    @RequestMapping("/listVOByCompId")
    @ResponseBody
    public RestResponseVO<List<CompetitionProblemVO>> listVOByCompId(Integer compId){
        return competitionProblemService.listVOByCompetitionId(compId);
    }

    @RequestMapping("/listVOByCompId1")
    @ResponseBody
    public RestResponseVO<List<CompetitionProblemVO>> listVOByCompId1(Integer compId){

        List<CompetitionProblemVO> competitionProblemVOList1 = null;
        competitionProblemVOList1 = get(compId,1);
        if(compId==null)
        {
            return RestResponseVO.createByErrorEnum(RestResponseEnum.INVALID_REQUEST);
        }
        return RestResponseVO.createBySuccess(competitionProblemVOList1);
    }

    @RequestMapping("/listVOByCompId2")
    @ResponseBody
    public RestResponseVO<List<CompetitionProblemVO>> listVOByCompId2(Integer compId){
        List<CompetitionProblemVO> competitionProblemVOList2 = null;
        competitionProblemVOList2 = get(compId,2);
        if(compId==null)
        {
            return RestResponseVO.createByErrorEnum(RestResponseEnum.INVALID_REQUEST);
        }
        return RestResponseVO.createBySuccess(competitionProblemVOList2);
    }

    @RequestMapping("/listVOByCompId3")
    @ResponseBody
    public RestResponseVO<List<CompetitionProblemVO>> listVOByCompId3(Integer compId){
        List<CompetitionProblemVO> competitionProblemVOList3 = null;
        competitionProblemVOList3 = get(compId,3);
        if(compId==null)
        {
            return RestResponseVO.createByErrorEnum(RestResponseEnum.INVALID_REQUEST);
        }
        return RestResponseVO.createBySuccess(competitionProblemVOList3);
    }

    public List<CompetitionProblemVO> get(Integer compId,int r)
    {
        List<CompetitionProblemVO> competitionProblemVOList = null;
        competitionProblemVOList = competitionProblemService.listVOByCompeId(compId);
        int n = 0,t = 0;
        if (competitionProblemVOList!=null&&!competitionProblemVOList.isEmpty())
        {
            n = competitionProblemVOList.size();
            t = n/3;
        }
        List<CompetitionProblemVO> competitionProblemVOList1 = new ArrayList<CompetitionProblemVO>();
        List<CompetitionProblemVO> competitionProblemVOList2 = new ArrayList<CompetitionProblemVO>();
        List<CompetitionProblemVO> competitionProblemVOList3 = new ArrayList<CompetitionProblemVO>();
        for(int i=0;i<t;++i)
        {
            competitionProblemVOList1.add(competitionProblemVOList.get(i));
        }
        for(int i=t;i<t+t;++i)
        {
            competitionProblemVOList2.add(competitionProblemVOList.get(i));
        }
        for(int i=t+t;i<n;++i)
        {
            competitionProblemVOList3.add(competitionProblemVOList.get(i));
        }
        if(r==1)
        {
            return competitionProblemVOList1;
        }
        else if(r==2)
        {
            return competitionProblemVOList2;
        }
        else
        {
            return competitionProblemVOList3;
        }
    }

}
